package scala

import scala.io.Source

object Demo24Student1 {
  def main(args: Array[String]): Unit = {


    /**
      *
      * 1、统计班级人数
      *
      */

    //1、读取数据
    val students: List[String] = Source.fromFile("data/students.txt").getLines().toList


    //2、取出班级
    val classz: List[String] = students.map(student => student.split(",")(4))


    //2、按照班级分组
    val group: Map[String, List[String]] = classz.groupBy(c => c)


    //统计班级的人数
    val clazzNum: Map[String, Int] = group.map(kv => {
      //班级
      val clazz: String = kv._1

      val clazzs: List[String] = kv._2

      //计算班级的人数
      val num: Int = clazzs.length

      (clazz, num)
    })

    val list: List[(String, Int)] = clazzNum.toList


    list.foreach(println)

  }
}
